package org.dromara.crawler.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.mongoplus.conditions.query.LambdaQueryChainWrapper;
import com.mongoplus.model.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.crawler.mongo.mongodomain.Demo;
import org.dromara.crawler.mongo.mongoservice.DemoService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * mongodb测试接口
 *
 * @author Shup
 * @date 2025/6/19 10:24
 */

@Slf4j
@RequiredArgsConstructor
//@RestController
@RequestMapping("/demo")
public class DemoController {

    private final DemoService demoService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/getList")
    public R<List<Demo>> getList() {
        return R.ok(demoService.list());
    }

    /**
     * mongodb新增
     *
     * @param demo
     * @return
     */
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody Demo demo) {
        return R.ok(demoService.save(demo));
    }


    /**
     * mongodb分页查询
     *
     * @param demo
     * @return
     */
    @GetMapping("/queryByPage")
    public TableDataInfo<Demo> queryByPage(Demo demo, PageQuery pageQuery) {
        LambdaQueryChainWrapper<Demo> queryChainWrapper = demoService.lambdaQuery().like(ObjectUtil.isNotEmpty(demo.getName()), Demo::getName, demo.getName());
        queryChainWrapper.orderByDesc(Demo::getId);
        PageResult<Demo> pageResult = demoService.page(queryChainWrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        TableDataInfo<Demo> build = TableDataInfo.build();
        build.setRows(BeanUtil.copyToList(pageResult.getContentData(), Demo.class));
        build.setTotal(pageResult.getTotalSize());
        return build;
    }


    /**
     * 收集日志
     *
     * @return
     */
    @PostMapping("/collectLogs")
    public R<Boolean> collectLogs(@RequestBody String message) {
        System.out.println(message);
        return R.ok();
    }

}
